package com.dailyblue.java.project.smbms.stock.listener;

import com.dailyblue.java.project.smbms.stock.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.SQLException;

@Component
@Slf4j
@RocketMQMessageListener(topic = "STOCK", selectorExpression = "UPDATE", consumerGroup = "zhuoyue02")
public class StockListener implements RocketMQListener<String> {
    @Resource
    private StockService stockService;

    @Override
    public void onMessage(String s) {
        if (s == null) {
            return;
        }
        String[] array = s.split(",");
        int goodsId = Integer.parseInt(array[0]);
        int num = Integer.parseInt(array[1]);
        log.info("开始更新库存信息，商品ID:{}，数量：{}", goodsId, num);
        try {
            stockService.updateStockNum(goodsId, 0-num);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
